package net.aiform.template.action;

import javax.servlet.http.HttpServletRequest;

import net.aiform.account.pojo.Person;
import net.aiform.account.service.PersonService;
import net.aiform.bean.Constant;
import net.aiform.template.pojo.Template;
import net.aiform.template.service.TemplateService;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;

public class TemplateAction implements Preparable, ServletRequestAware {
	private Logger log = Logger.getLogger(this.getClass());
	private PersonService personService;
	private TemplateService templateService;
	private HttpServletRequest req;
	private Person person;
	private String certPicCode;
	private String arg1;
	private Template template;

	public String saveNewTemplate() {
		return Action.SUCCESS;
	}

	public String buildNewTemplate() {
		//String code = req.getServletPath().substring(req.getServletPath().lastIndexOf("/") + 1);
		String code = template.getCode();
		System.out.println(code);
		this.templateService.save(template);
		return Action.SUCCESS;
	}

	public String designNewTemplate() {
		return Action.SUCCESS;
	}

	public String createNewTemplate() {
		log.info("create new template");
		Person p = (Person) req.getSession().getAttribute(Constant.LOGINUSER);
		if (!(p instanceof Person)) return Action.ERROR;
		Template t = templateService.getNewTemplate();
		this.req.getSession().setAttribute(Constant.FORMTEMPLATE, t);
		return Action.SUCCESS;
	}

	public String execute() {
		return Action.SUCCESS;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public String getCertPicCode() {
		return certPicCode;
	}

	public void setCertPicCode(String certPicCode) {
		this.certPicCode = certPicCode;
	}

	public TemplateAction(PersonService personService, TemplateService templateService) {
		this.personService = personService;
		this.templateService = templateService;
	}

	public void prepare() throws Exception {

	}

	@JSON(serialize = false)
	public String getArg1() {
		return arg1;
	}

	public void setArg1(String arg1) {
		this.arg1 = arg1;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.req = request;
	}

	public TemplateService getTemplateService() {
		return templateService;
	}

	public void setTemplateService(TemplateService templateService) {
		this.templateService = templateService;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public Template getTemplate() {
		return template;
	}

	public void setTemplate(Template template) {
		this.template = template;
	}
}
